package com.example.front.main.the_order;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.front.MyApplication;
import com.example.front.R;
import com.example.front.main.community.Room;
import com.example.front.main.community.RoomActivity;
import com.example.front.main.community.RoomCreateActivity;
import com.example.front.main.community.RoomIntroductionActivity;
import com.example.front.main.shop.SelectProduct;
import com.example.front.utils.MyAdapter;
import com.example.front.utils.ShopAdapter;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okio.BufferedSink;

public class OrderActivity extends AppCompatActivity {
    private String URL;
    private MyApplication myApplication;
    private ListView order_listView;
    private MyAdapter<Order> orderMyAdapter = null;
    private List<Order> orderList = null;
    private TextView noOrder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order);
        myApplication = (MyApplication) getApplication();
        order_listView = findViewById(R.id.order_listView);
        noOrder = findViewById(R.id.noOrder);
        URL = myApplication.getURL();
        SharedPreferences sharedPreferences = getSharedPreferences("Login", Context.MODE_PRIVATE);
        String account = sharedPreferences.getString("account", "");
        OkHttpUtils.post()
                .url(URL + "/order/getOrderList")
                .addParams("account",account)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {
                        Toast.makeText(OrderActivity.this, "网络错误", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(String response) {
                        String json = response;
                        Log.i("success", "获取成功！");
                        orderList = new ArrayList<Order>();
                        try {
                            JSONArray jsonArray = new JSONArray(json);
                            if(jsonArray.length() == 0){
                                noOrder.setVisibility(View.VISIBLE);
                                order_listView.setVisibility(View.GONE);
                            }else
                            {
                                for (int i = 0; i < jsonArray.length(); i++){
                                    JSONObject jsonObject = (JSONObject) jsonArray.get(i);
                                    Order order = new Order();
                                    order.setOrderID(jsonObject.getInt("orderID"));
                                    order.setAccount(jsonObject.getString("account"));
                                    order.setRoom(jsonObject.getString("room"));
                                    order.setSubmitTime(jsonObject.getString("submitTime"));
                                    order.setState(jsonObject.getInt("state"));
                                    order.setCost(jsonObject.getDouble("cost"));
                                    orderList.add(order);
                                }
                            }
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                        orderMyAdapter = new MyAdapter<Order>((ArrayList) orderList, R.layout.order_list_item){
                            @Override
                            public void bindView(ViewHolder holder, Order obj){
                                holder.setText(R.id.room_textView, obj.getRoom() + "房");
                                holder.setText(R.id.time_textView, obj.getSubmitTime() + "");
                                holder.setText(R.id.cost_textView, "￥" + obj.getCost());
                                if(obj.getState() != 1)holder.setText(R.id.state_textView, "待完成");
                                else holder.setText(R.id.state_textView, "已完成");
                            }
                        };
                        order_listView.setAdapter(orderMyAdapter);
                        order_listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                Order order = orderList.get(position);
                                Intent intent = new Intent(OrderActivity.this, OrderProductActivity.class);
                                Bundle bundle = new Bundle();
                                bundle.putInt("orderID", order.getOrderID());
                                bundle.putString("room", order.getRoom());
                                bundle.putDouble("cost", order.getCost());
                                intent.putExtras(bundle);
                                startActivity(intent);
                            }
                        });
                    }
                });
    }
}
